/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the IncrementalParser package.
 *  
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.incparser.main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.osdtsystem.incparser.logging.Log;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class Options {
    static Map<String,String> options = defaultOptions();
    static Map<String,Level> levels = levels();

    static Map<String,Level> levels() {
        Map<String,Level> newLevels = new HashMap<String,Level>();
        newLevels.put("ALL", Level.ALL);
        newLevels.put("CONFIG", Level.CONFIG);
        newLevels.put("FINE", Level.FINE);
        newLevels.put("FINER", Level.FINER);
        newLevels.put("FINEST", Level.FINEST);
        newLevels.put("INFO", Level.INFO);
        newLevels.put("OFF", Level.OFF);
        newLevels.put("SEVERE", Level.SEVERE);
        newLevels.put("WARNING", Level.WARNING);
        return(newLevels);
    }

    static Map<String,String> defaultOptions() {
        Map<String,String> defaults = new HashMap<String,String>();
        defaults.put("train.iterations", "5");
        defaults.put("train.iterations1", "0");
        defaults.put("train.loglevel.console", "CONFIG");
        defaults.put("train.loglevel.html", "NONE");
        defaults.put("eval.loglevel.console", "CONFIG");
        defaults.put("eval.loglevel.html", "NONE");
        defaults.put("eval.logdir", "html.logs");
        defaults.put("train.file", "train.conll");
        defaults.put("eval.file", "eval.conll");
        defaults.put("eval.output1", "eval-parse1.conll");
        defaults.put("eval.output2", "eval-parse.conll");
        return defaults;
    }

    public static void parseArgs(String[] args) {
        int pos = 0;
        int nonopts = 0;
        while (pos < args.length) {
            if (args[pos].substring(0, 1).equals("-")) {
                // Find first "=" in string
                int eqpos = args[pos].indexOf("=");
                String key, value = null;
                if (eqpos > 0) {
                    key = args[pos].substring(1, eqpos);
                    if (options.containsKey(key)) {
                        value = args[pos].substring(eqpos + 1);
                        options.put(key, value);
                    }
                }
                if (value == null)
                    Log.severe("Unknown option: " + args[pos]);
            } else if (nonopts == 0) {
                options.put("train.file", args[pos]);
                ++nonopts;
            } else if (nonopts == 1) {
                options.put("eval.file", args[pos]);
                ++nonopts;
            } else {
                Log.severe("Invalid command-line argument: " + args[pos]);
            }
            ++pos;
        }

        // Log options
        StringBuilder s = new StringBuilder("Options\n");
        List<String> keys = new ArrayList<String>(options.keySet());
        Collections.sort(keys);
        for (String key : keys) {
            s.append(key);
            s.append("=");
            s.append(options.get(key));
            s.append("\n");
        }
        Log.config(s);
    }

    public static String option(String key) {
        return options.get(key);
    }
    
    public static int intOption(String key) {
        return Integer.parseInt(options.get(key));
    }

    public static boolean booleanOption(String key) {
        return Boolean.parseBoolean(options.get(key));
    }

    public static Level levelOption(String key) {
        Level result = levels.get(key);
        return result == null ? Level.CONFIG : result;
    }
}
